<?php
/**
 * 地址管理
 * 2012-12-25 by huanran.zhao
 */
class AddressModel {

	/**
	 * 得到与地址关联的对应变量
	 * @param array $get $_GET参数
	 * @return array
	 */
	public function getRelationVar($get){
		$result['province']=$result['city']=$result['area']=$result['street']='';
		//省
		if(isset($get['province'])){
			$result['province'] = $get['province'];
			if($get['province']=='')	$result['city']=$result['area']=$result['street']=0;
		}
		//市
		if(isset($get['city'])){
			$province = self::cityId2provinceId($get['city']);
			//$_GET[city]对应的省 != $_GET[province] AND $_GET[province]为空
			if($province!=$get['province'] AND $get['province']==''){
				$result['province'] = $province;
			}else $result['province'] = $get['province'];
			$result['city']=$get['city'];
			$result['area']=$result['street']=0;
		}
		//区
		if(isset($get['area']) AND My::is_id($get['area'])){
			$result['area'] = $get['area'];
			$result['city'] = self::areaId2cityId($get['area']);
			$result['province'] = self::areaId2provinceId($get['area']);		
		}
		//街镇
		if(isset($get['street']) AND My::is_id($get['street'])){
			$result['street'] = $get['street'];
			$result['area'] = self::streetId2areaId($get['street']);
			$result['city'] = self::streetId2cityId($get['street']);
			$result['province'] = self::streetId2provinceId($get['street']);
		}

		/*//清空相应项
		if($result['province']!=$get['province']){//省onchange
			//$citys = self::provinceId2city($result['province']);
			//$ids = My::arr2one($citys,'city_id');
			//if($ids)
			$result['city']=$result['area']=$result['street']=0;
		}
		if($result['city']!=$get['city']) $result['area']=$result['street']=0;
		if($result['area']!=$get['area']) $result['street']=0;*/
		return $result;
	}

	/**
	 * 区域ID->街道列表
	 * @param int id 区域ID
	 * @param int publish 是否发布(可选,为空时取所有状态)
	 * @return int
	 */
	public function areaId2street($id=0,$publish=''){
	    $where = "1 ";
	    if($publish!='') $where .= "AND `publish`={$publish} ";
		if(My::is_id($id)) $where .= "AND `area_id`={$id} ";
		$result = DB('address_street')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 省ID->街镇列表
	 * @param int id 省ID
	 * @return int
	 */
	public function provinceId2street($id=0){
		$id=(int)$id;
		//得到当前省对应的所有区域列表
		$areas = self::provinceId2area($id);
		$areas = My::arr2one($areas,'area_id');
		$where = (is_array($areas) AND $areas)?'area_id IN('.implode(',',$areas).')':'1';
		$result = DB('address_street')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 城市ID->街道列表
	 * @param int id 城市ID
	 * @return int
	 */
	public function cityId2street($id=0){
		$id=(int)$id;
		//得到当前城市对应的所有区域列表
		$areas = self::cityId2area($id);
		$areas = My::arr2one($areas,'area_id');
		$where = (is_array($areas) AND $areas)?'area_id IN('.implode(',',$areas).')':'1';

		$result = DB('address_street')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 省ID->区域列表
	 * @param int id 省ID
	 * @return int
	 */
	public function provinceId2area($id=0){
		$id=(int)$id;
		//得到当前省对应的所有城市列表
		$citys = self::provinceId2city($id);
		$citys = My::arr2one($citys,'city_id');
		$where = (is_array($citys) AND $citys)?'city_id IN('.implode(',',$citys).')':'1';

		$result = DB('address_area')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 城市ID->区域列表
	 * @param int id 城市ID
	 * @param int publish 是否发布(可选,为空时取所有状态)
	 * @return int
	 */
	public function cityId2area($id=0,$publish=''){
	    $where = "1 ";
	    if($publish!='') $where .= "AND `publish`={$publish} ";
		if(My::is_id($id)) $where .= "AND `city_id`={$id} ";
		$result = DB('address_area')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 省ID->城市列表
	 * @param int id 省ID
	 * @param int publish 是否发布(可选,为空时取所有状态)
	 * @return int
	 */
	public function provinceId2city($id=0,$publish=''){
	    $where = "1 ";
	    if($publish!='') $where .= "AND `publish`={$publish} ";
		if(My::is_id($id)) $where .= "AND `province_id`={$id} ";
		$result = DB('address_city')->where($where)->cache()->select();
		return $result;
	}

	/**
	 * 城市id->省id
	 * @param int id 城市ID
	 * @return int
	 */
	public function cityId2provinceId($id=0){
		if(!My::is_id($id)) return false;
		$result = DB('address_city')->where("city_id={$id}")->cache()->getField('province_id');
		return $result;
	}

	/**
	 * 区域id->省id
	 * @param int id 区域ID
	 * @return int
	 */
	public function areaId2provinceId($id=0){
		if(!My::is_id($id)) return false;
		$cityId = self::areaId2cityId($id);
		$result = self::cityId2provinceId($cityId);
		return $result;
	}

	/**
	 * 区域id->城市id
	 * @param int id 区域ID
	 * @return int
	 */
	public function areaId2cityId($id=0){
		if(!My::is_id($id)) return false;
		$result = DB('address_area')->where("area_id={$id}")->cache()->getField('city_id');
		return $result;
	}

	/**
	 * 街镇id->省id
	 * @param int id 省ID
	 * @return int
	 */
	public function streetId2provinceId($id=0){
		if(!My::is_id($id)) return false;
		$areaId = self::streetId2areaId($id);
		$result = self::areaId2provinceId($areaId);
		return $result;
	}

	/**
	 * 街镇id->城市id
	 * @param int id 城市ID
	 * @return int
	 */
	public function streetId2cityId($id=0){
		if(!My::is_id($id)) return false;
		$areaId = self::streetId2areaId($id);
		$result = self::areaId2cityId($areaId);
		return $result;
	}

	/**
	 * 街镇id->区域id
	 * @param int id 街镇ID
	 * @return int
	 */
	public function streetId2areaId($id=0){
		if(!My::is_id($id)) return false;
		$result = DB('address_street')->where("street_id={$id}")->cache()->getField('area_id');
		return $result;
	}
   
	/**
	 * 得到所有省(或一个省)
	 * @param int id 省ID(可选,为空时取所有省)
	 * @param int publish 是否发布(可选,为空时取所有状态)
	 * @return array
	 */
	public function getProvince($id=0,$publish=''){
	    $where = "1 ";
	    if($publish!='') $where .= "AND `publish`={$publish} ";
		if(My::is_id($id)) $where .= "AND `province_id`={$id} ";
		$result = DB('address_province')->where($where)->cache()->order("`ordering` ASC")->select();
		return $result;
	}
	  
	/**
	 * 得到所有市(或一个市)
	 * @param int id 市ID(可选,为空时取所有市)
	 * @return array
	 */
	public function getCity($id=0){
		$where = "1 ";
		if(My::is_id($id)) $where .= "AND `city_id`={$id} ";
		$result = DB('address_city')->where($where)->cache()->order("`ordering` ASC")->select();
		return $result;
	}

	/**
	 * 得到所有区(或一个区)
	 * @param int id 区ID(可选,为空时取所有区)
	 * @return array
	 */
	public function getArea($id=0){
		$where = "1 ";
		if(My::is_id($id)) $where .= "AND `area_id`={$id} ";
		$result = DB('address_area')->where($where)->cache()->order("`ordering` ASC")->select();
		return $result;
	}

	/**
	 * 得到所有街镇(或一个街镇)
	 * @param int id 街镇ID(可选,为空时取所有街镇)
	 * @return array
	 */
	public function getStreet($id=0){
		$where = "1 ";
		if(My::is_id($id)) $where .= "AND `street_id`={$id} ";
		$result = DB('address_street')->where($where)->cache()->order("`ordering` ASC")->select();
		return $result;
	}

}